package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.core.RemoteConfig;
import com.google.common.collect.Lists;
import com.rules.admin.dao.RemoteModelDao;
import com.rules.admin.service.RemoteService;
import com.rules.admin.entity.RemoteModel;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class RemoteServiceImpl extends ServiceImpl<RemoteModelDao, RemoteModel> implements RemoteService {
   public IPage<RemoteModel> queryPage(RemoteModel remoteModel, IPage<RemoteModel> page) {
      LambdaQueryWrapper<RemoteModel> queryWrapper = new LambdaQueryWrapper<>();


      if (remoteModel.getProjectId() != null && !"".equals(remoteModel.getProjectId())) {
         queryWrapper.eq(RemoteModel::getProjectId, remoteModel.getProjectId());

      }

      if (remoteModel.getRemoteCode() != null && !"".equals(remoteModel.getRemoteCode())) {
         queryWrapper.eq(RemoteModel::getRemoteCode, remoteModel.getRemoteCode());

      }

      if (remoteModel.getRemoteName() != null && !"".equals(remoteModel.getRemoteName())) {
         queryWrapper.eq(RemoteModel::getRemoteName, remoteModel.getRemoteName());

      }
      return this.page(page, queryWrapper);
   }

   public List<RemoteConfig> selectListByProjectCode(String projectCode) {
      LambdaQueryWrapper<RemoteModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RemoteModel::getProjectCode, projectCode);
      List<RemoteModel> remoteModelList = this.baseMapper.selectList(queryWrapper);

      List<RemoteConfig> remoteConfigsList = Lists.newArrayList();
      Iterator iterator = remoteModelList.iterator();

      while(iterator.hasNext()) {
         RemoteModel remoteModel = (RemoteModel)iterator.next();
         RemoteConfig remoteConfig = new RemoteConfig();
         remoteConfig.setRemoteName(remoteModel.getRemoteName());
         remoteConfig.setRemoteCode(remoteModel.getRemoteCode());
         remoteConfig.setRemoteConfig(remoteModel.getRemoteConfig());
         remoteConfig.setRemoteParams(remoteModel.getRemoteParams());
         remoteConfig.setRemoteResult(remoteModel.getRemoteResult());
         remoteConfigsList.add(remoteConfig);
      }

      return remoteConfigsList;
   }
}
